// pages/square/square.js
var util = require("../../utils/util.js"),
  api = require("../../utils/api.js"),
  app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    configInfo:{},
    showShare:false,
    options: [
      { name: '微信分享', icon: 'wechat', openType: 'share' },
      { name: '复制pdf链接', icon: 'link' },
      { name: '下载pdf', icon: 'poster' } 
    ],
    active:0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onShow(options) {
    let openid=wx.getStorageSync('openid');
    if(openid){
      this.getConfig()
    }else{
      api.default.toLogin().then(res => {
        this.getConfig();
      })
    }
  },
  getConfig(){
    api.default.config().then((a) => {
      this.setData({
        configInfo: a.data
      })
    });
  },
  onChange(e) {
    this.setData({
      active:e.detail.index
    })
  },
  onClose(){
    this.setData({
      showShare: false
    })
  },
  gotoNext(){
    this.setData({
      showShare: true
    })
  },
  onSelect(e){
    const {index}=e.detail;
    if(!this.data.configInfo.claim_materials_pdf) return;
    let url=app.globalData.baseImgUrl + this.data.configInfo.claim_materials_pdf;
    if(index == 2){
      wx.downloadFile({
        url: url,
        success: function (resinfo) {
          let path = resinfo.tempFilePath;
          wx.openDocument({
            filePath: path,
            fileType: 'pdf',
            success: function (rest) {
              console.log(rest);
            },
            fail: function (error) {
              console.log('error',error)
              wx.showToast({
                icon: 'none',
                title: '打开文件失败'
              });
            },
          })
        },
        fail: function (err) {
          wx.showToast({
            icon: 'none',
            title: '下载文件失败'
          });
        }
      })
    }else if(index == 1){
      wx.setClipboardData({
        data: url,
        success: function() {
          wx.showToast({
            title: '链接已复制',
            icon: 'success',
            duration: 2000
          })
        }
      })
    }
    this.onClose()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
   
  },
  onShareAppMessage(e) {
    return util.shareEvent(e)
  }
})